<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="com.example.domain.Comment" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>评论管理 - 论坛系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #f8fafc 0%, #e0f2fe 100%);
            min-height: 100vh;
            color: #374151;
        }

        .header {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            border-bottom: 1px solid rgba(255, 255, 255, 0.2);
            position: sticky;
            top: 0;
            z-index: 100;
        }

        .header-content {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 16px;
        }

        .header h1 {
            font-size: 28px;
            font-weight: bold;
            background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .back-btn {
            background: linear-gradient(135deg, #6b7280 0%, #4b5563 100%);
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 25px;
            text-decoration: none;
            font-weight: 600;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .back-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(107, 114, 128, 0.4);
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 30px 20px;
        }

        .table-container {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            border: 1px solid rgba(255, 255, 255, 0.2);
        }

        .table-header {
            background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }

        .table-header h2 {
            font-size: 24px;
            font-weight: 700;
            margin-bottom: 8px;
        }

        .table-header p {
            opacity: 0.9;
            font-size: 16px;
        }

        .table-wrapper {
            overflow-x: auto;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        th, td {
            padding: 20px 16px;
            text-align: left;
            border-bottom: 1px solid rgba(229, 231, 235, 0.5);
        }

        th {
            background: rgba(248, 250, 252, 0.8);
            font-weight: 600;
            color: #374151;
            font-size: 14px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        td {
            color: #4b5563;
            font-size: 15px;
        }

        tr:hover {
            background: rgba(248, 250, 252, 0.5);
        }

        .status-badge {
            padding: 8px 16px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .status-active {
            background: linear-gradient(135deg, #d1fae5 0%, #a7f3d0 100%);
            color: #065f46;
        }

        .status-blocked {
            background: linear-gradient(135deg, #fee2e2 0%, #fecaca 100%);
            color: #991b1b;
        }

        .comment-content {
            max-width: 300px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            background: rgba(248, 250, 252, 0.5);
            padding: 8px 12px;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .comment-content:hover {
            background: rgba(229, 231, 235, 0.5);
            white-space: normal;
            max-width: none;
        }

        .action-buttons {
            display: flex;
            gap: 8px;
            flex-wrap: wrap;
        }

        .action-btn {
            padding: 8px 16px;
            border: none;
            border-radius: 12px;
            font-size: 13px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 6px;
        }

        .btn-toggle {
            background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
            color: white;
        }

        .btn-delete {
            background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
            color: white;
        }

        .action-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
        }

        .empty-state {
            text-align: center;
            padding: 80px 20px;
            color: #6b7280;
        }

        .empty-state-icon {
            font-size: 64px;
            margin-bottom: 24px;
            opacity: 0.5;
        }

        .empty-state h3 {
            font-size: 24px;
            color: #374151;
            margin-bottom: 12px;
        }

        .empty-state p {
            font-size: 16px;
            line-height: 1.6;
        }

        .confirm-dialog {
            background: rgba(0, 0, 0, 0.6);
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            display: none;
            align-items: center;
            justify-content: center;
            z-index: 1000;
            backdrop-filter: blur(4px);
        }

        .confirm-content {
            background: white;
            padding: 40px;
            border-radius: 20px;
            text-align: center;
            max-width: 400px;
            margin: 20px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
        }

        .confirm-content h3 {
            font-size: 20px;
            color: #374151;
            margin-bottom: 12px;
        }

        .confirm-content p {
            color: #6b7280;
            margin-bottom: 24px;
            line-height: 1.6;
        }

        .confirm-buttons {
            display: flex;
            gap: 12px;
            justify-content: center;
        }

        .id-badge {
            background: linear-gradient(135deg, #f3f4f6 0%, #e5e7eb 100%);
            color: #374151;
            padding: 4px 8px;
            border-radius: 6px;
            font-size: 12px;
            font-weight: 600;
        }

        @media (max-width: 768px) {
            .header-content {
                flex-direction: column;
                text-align: center;
            }

            .comment-content {
                max-width: 200px;
            }

            .action-buttons {
                flex-direction: column;
            }

            .action-btn {
                width: 100%;
                justify-content: center;
            }

            th, td {
                padding: 12px 8px;
                font-size: 13px;
            }
        }

        @media (max-width: 480px) {
            .container {
                padding: 20px 16px;
            }

            .table-header {
                padding: 20px;
            }

            .table-header h2 {
                font-size: 20px;
            }

            .confirm-content {
                padding: 30px 20px;
            }
        }
    </style>
</head>
<body>
<div class="header">
    <div class="header-content">
        <h1>
            <span>💬</span>
            <span>评论管理</span>
        </h1>
        <a href="/admin" class="back-btn">
            <span>🏠</span>
            <span>返回管理首页</span>
        </a>
    </div>
</div>

<div class="container">
    <div class="table-container">
        <div class="table-header">
            <h2>评论列表</h2>
            <p>管理所有用户评论，维护良好的讨论环境</p>
        </div>

        <%
            List<Comment> comments = (List<Comment>) request.getAttribute("comments");
            if (comments != null && !comments.isEmpty()) {
        %>
        <div class="table-wrapper">
            <table>
                <thead>
                <tr>
                    <th>ID</th>
                    <th>用户ID</th>
                    <th>帖子ID</th>
                    <th>内容</th>
                    <th>状态</th>
                    <th>时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <% for (Comment c : comments) { %>
                <tr>
                    <td><span class="id-badge"><%= c.getId() %></span></td>
                    <td><span class="id-badge"><%= c.getUserId() %></span></td>
                    <td><span class="id-badge"><%= c.getPostId() %></span></td>
                    <td>
                        <div class="comment-content" title="<%= c.getContent() %>">
                            <%= c.getContent() %>
                        </div>
                    </td>
                    <td>
                        <span class="status-badge status-<%= c.getStatus() %>">
                            <%= "active".equals(c.getStatus()) ? "正常" : "屏蔽" %>
                        </span>
                    </td>
                    <td><%= c.getCreateTime() %></td>
                    <td>
                        <div class="action-buttons">
                            <form action="/admin/updateCommentStatus" method="post" style="display:inline;">
                                <input type="hidden" name="id" value="<%= c.getId() %>"/>
                                <input type="hidden" name="status" value="<%= "active".equals(c.getStatus()) ? "blocked" : "active" %>"/>
                                <button type="submit" class="action-btn btn-toggle">
                                    <span><%= "active".equals(c.getStatus()) ? "🚫" : "✅" %></span>
                                    <span><%= "active".equals(c.getStatus()) ? "屏蔽" : "解除屏蔽" %></span>
                                </button>
                            </form>
                            <button type="button" class="action-btn btn-delete" onclick="confirmDelete(<%= c.getId() %>)">
                                <span>🗑️</span>
                                <span>删除</span>
                            </button>
                        </div>
                    </td>
                </tr>
                <% } %>
                </tbody>
            </table>
        </div>
        <% } else { %>
        <div class="empty-state">
            <div class="empty-state-icon">💬</div>
            <h3>暂无评论数据</h3>
            <p>论坛中还没有用户评论</p>
        </div>
        <% } %>
    </div>
</div>

<!-- 确认删除对话框 -->
<div id="confirmDialog" class="confirm-dialog">
    <div class="confirm-content">
        <h3>⚠️ 确认删除</h3>
        <p>您确定要删除这条评论吗？此操作不可恢复。</p>
        <div class="confirm-buttons">
            <button type="button" class="action-btn btn-toggle" onclick="closeConfirmDialog()">
                <span>❌</span>
                <span>取消</span>
            </button>
            <button type="button" class="action-btn btn-delete" onclick="executeDelete()">
                <span>✅</span>
                <span>确认删除</span>
            </button>
        </div>
    </div>
</div>

<script>
    let deleteCommentId = null;

    function confirmDelete(commentId) {
        deleteCommentId = commentId;
        document.getElementById('confirmDialog').style.display = 'flex';
    }

    function closeConfirmDialog() {
        document.getElementById('confirmDialog').style.display = 'none';
        deleteCommentId = null;
    }

    function executeDelete() {
        if (deleteCommentId) {
            const form = document.createElement('form');
            form.method = 'post';
            form.action = '/admin/deleteComment';

            const input = document.createElement('input');
            input.type = 'hidden';
            input.name = 'id';
            input.value = deleteCommentId;

            form.appendChild(input);
            document.body.appendChild(form);
            form.submit();
        }
    }

    // 点击对话框外部关闭
    document.getElementById('confirmDialog').addEventListener('click', function(e) {
        if (e.target === this) {
            closeConfirmDialog();
        }
    });

    // ESC键关闭对话框
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Escape') {
            closeConfirmDialog();
        }
    });
</script>
</body>
</html>
